package com.jinqing.wuchuang.service.impl;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.jinqing.wuchuang.entity.Login;
import com.jinqing.wuchuang.entity.SettleCompany;
import com.jinqing.wuchuang.entity.User;
import com.jinqing.wuchuang.mapper.AuthUserMapper;
import com.jinqing.wuchuang.mapper.LoginMapper;
import com.jinqing.wuchuang.mapper.UserMapper;
import com.jinqing.wuchuang.response.RespBean;
import com.jinqing.wuchuang.service.AuthUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Service
public class AuthUserServiceImpl implements AuthUserService {
    /**
     * 生成验证码
     * @return
     */
    @Autowired
    private DefaultKaptcha captchaProducer;
    @Autowired
    private AuthUserMapper authUserMapper;
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public Map<String, Object> GenerateVerificationCode() throws IOException {
        String capText = captchaProducer.createText();
        // create the image with the text
        BufferedImage bi = captchaProducer.createImage(capText);
        //将BufferedImage转换为字节数组，给前端返回
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write( bi, "jpg", baos );
        byte[] imageInByte = null;
        try {
            baos.flush();
            //使用toByteArray()方法转换成字节数组
            imageInByte = baos.toByteArray();
        } finally {
            baos.close();
        }
        HashMap<String, Object> result = new HashMap<String, Object>();
        //给前端返回code，标识图片,前端验证时发回来
        result.put("code", UUID.randomUUID());
        result.put("image",imageInByte);
        return result;
    }

    /**
     * 验证验证码，返回Boolean标识对错
     * @param code
     * @param value
     * @return
     */
    @Override
    public boolean identifyingCode(String code, String value) {
        return false;
    }

    @Override
    public Long getUidByuserName(String userName) {
        return authUserMapper.getUidByUserName(userName);
    }
    /**
     * 用户注册，插入两个表，使用事务
     * @param login
     * @throws Exception
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void register(Login login) throws Exception {
        //根据login的信息，获取身份，目前只有学生，后期需修改
        User u = new User();
        u.setRoleName("ROLE_stu");
        login.setPassword(passwordEncoder.encode(login.getPassword()));
        login.setUid(u.getUid());
        userMapper.insert(u);
        login.setUid(u.getUid());
        loginMapper.insert(login);
    }

    @Override
    public Long getUidByToken() {
        String userName =(String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return getUidByuserName(userName);
    }




}
